package com.quasar.hpatient.module.mine_case_edit;

import android.content.Intent;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.quasar.hpatient.R;
import com.quasar.hpatient.bean.comm_album.AlbumPhotoBean;
import com.quasar.hpatient.dialog.DateDialog;
import com.quasar.hpatient.dialog.RadioDoubleDialog;
import com.quasar.hpatient.module.comm_album.AlbumActivity;
import com.quasar.hpatient.module.comm_camera.CameraActivity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import lib.quasar.base.frame.BaseActivity;
import lib.quasar.db.manager.DBManager;
import lib.quasar.db.table.Aerainfo;
import lib.quasar.db.table.Hospital;
import lib.quasar.widget.menu.NavigationView;
import lib.quasar.widget.photo.PhotoLayout;
import lib.quasar.widget.photo.PhotoModel;

/**
 * description: 我 ==> 添加病例
 * created by kalu on 2018/7/17 18:44
 */
public final class MineCaseEditActivity extends BaseActivity<MineCaseEditPresenter> implements MineCaseEditView {

    public final static int REQUEST_CODE = 1001;
    public final static int RESULT_CODE = 2222;

    public final static String CASE_ID = "CASE_ID";
    public final static String CASE_NAME = "CASE_NAME";
    public final static String CASE_DATE = "CASE_DATE";
    public final static String CASE_TITLE = "CASE_TITLE";
    public final static String CASE_INSPECTION = "CASE_INSPECTION";
    public final static String CASE_PICTURE = "CASE_PICTURE";

    @Override
    public int initView() {
        return R.layout.activity_mine_case_edit;
    }

    @Override
    public void initDataLocal() {

        final EditText title = findViewById(R.id.mine_case_add_title);
        final String titles = getIntent().getStringExtra(CASE_TITLE);
        title.setText(titles);

        final TextView hospital = findViewById(R.id.mine_case_add_hospital);
        final String hospitals = getIntent().getStringExtra(CASE_NAME);
        hospital.setText(hospitals);
        hospital.setOnClickListener(v -> showHospital(hospital, "选择医院"));

        final TextView date = findViewById(R.id.mine_case_add_date);
        final String dates = getIntent().getStringExtra(CASE_DATE);
        date.setText(dates);
        date.setOnClickListener(v -> showDate(date, "选择日期"));

        final TextView inspection = findViewById(R.id.mine_case_add_inspection);
        final String inspections = getIntent().getStringExtra(CASE_INSPECTION);
        inspection.setText(inspections);

        final RecyclerView recycler = findViewById(R.id.mine_case_add_picture);
        getPresenter().initList(this, recycler);
        final ArrayList<String> list = getIntent().getStringArrayListExtra(CASE_PICTURE);
        getPresenter().refreshPicture(this, recycler, list);

        final NavigationView menu = findViewById(R.id.mine_case_add_menu);
        menu.setOnNavigationChangeListener((isBack, isMenu) -> {
            if (isBack) onBackPressed();
            else {
                final int id = getIntent().getIntExtra(CASE_ID, -1);
                getPresenter().pushCase(MineCaseEditActivity.this, title, id,
                        title.getText().toString(), hospital.getText().toString(), date.getText().toString(), inspection.getText().toString());
            }
        });
    }

    @Override
    public void showAlbum(List<AlbumPhotoBean> mPhoto) {

        final Intent intent = new Intent(getApplicationContext(), AlbumActivity.class);
        intent.putExtra(AlbumActivity.LIST, (Serializable) mPhoto);
        intent.putExtra(AlbumActivity.CAMERA, true);
        startActivityForResult(intent, REQUEST_CODE);
    }

    @Override
    public void showPhoto(int defaultPosition, List<? extends PhotoModel> imageLittleUrlList, List<? extends PhotoModel> imageUrlList, List<ImageView> imageViews) {

        if (imageLittleUrlList.isEmpty() || imageUrlList.isEmpty() || imageViews.isEmpty())
            return;

        new PhotoLayout.Builder(this)
                .setPhotoLongPressSave(false)
                .setPhotoOpenTransAnim(false)
                .setPhotoDefaultPosition(defaultPosition)
                .setPhotoBackgroundColor(Color.BLACK)
                .setPhotoDefaultResource(R.mipmap.ic_launcher)
                .setPhotoViewList(imageViews)
                .setPhotoUrlList(imageUrlList)
                .setPhotoLittleUrlList(imageLittleUrlList)
                .show();
    }

    @Override
    public void callBack(ArrayList<String> list) {

        final EditText title = findViewById(R.id.mine_case_add_title);
        final String titles = title.getText().toString();
        final TextView hospital = findViewById(R.id.mine_case_add_hospital);
        final String hospitals = hospital.getText().toString();
        final TextView date = findViewById(R.id.mine_case_add_date);
        final String dates = date.getText().toString();
        final TextView inspection = findViewById(R.id.mine_case_add_inspection);
        final String inspections = inspection.getText().toString();

        final Intent intent = new Intent();
        intent.putExtra(CASE_ID, getIntent().getIntExtra(CASE_ID, -1));
        intent.putExtra(CASE_NAME, hospitals);
        intent.putExtra(CASE_DATE, dates);
        intent.putExtra(CASE_TITLE, titles);
        intent.putExtra(CASE_INSPECTION, inspections);
        intent.putExtra(CASE_PICTURE, list);
        setResult(RESULT_CODE, intent);

        onBackPressed();
    }

    @Override
    public void showDate(TextView view, String title) {

        final DateDialog dialog = new DateDialog(this);
        dialog.show();
        dialog.setTitles(title);
        dialog.setOnDateChangeListener((year, month, day) -> view.setText(year + "-" + month + "-" + day));
    }

    @Override
    public void showHospital(TextView view, String title) {

        final List<String> cityList = new ArrayList<>();
        final List<String> hospitalList = new ArrayList<>();
        final RadioDoubleDialog dialog = new RadioDoubleDialog(this);

        dialog.setOnDialogChangeListener((str1, str2, iswheel) -> {
            if (!iswheel) {
                setText(view, str2);
            } else if (!TextUtils.isEmpty(str1)) {
                String areaCode = DBManager.getInstance().getAreaCode(str1);
                final List<Hospital> list = DBManager.getInstance().getHospitalList(areaCode);
                hospitalList.clear();
                for (Hospital temp : list) {
                    hospitalList.add(temp.getName());
                }
                dialog.setList(null, hospitalList);
            }
        });
        dialog.show();

        final List<Aerainfo> list = DBManager.getInstance().getProvinceList();
        for (Aerainfo temp1 : list) {
            cityList.add(temp1.getAeraInfoName());
        }
        dialog.setList(cityList, null);
        dialog.setTitle(title);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == AlbumActivity.RESULT_CODE_OPEN_CAMERA) {
            Intent intent = new Intent(getApplicationContext(), CameraActivity.class);
            startActivityForResult(intent, REQUEST_CODE);
        } else if (resultCode == AlbumActivity.RESULT_CODE && null != data) {
            ArrayList<AlbumPhotoBean> list = data.getParcelableArrayListExtra(AlbumActivity.LIST);
            if (null == list || list.size() == 0) return;
            final RecyclerView recycler = findViewById(R.id.mine_case_add_picture);
            getPresenter().compressPhoto(this, recycler, list, -1);
        } else if (resultCode == CameraActivity.RESULT_CODE && null != data) {
            final String path = data.getStringExtra(CameraActivity.PATH);
            final long size = data.getLongExtra(CameraActivity.SIZE, -1);
            final int width = data.getIntExtra(CameraActivity.WIDTH, -1);
            final int height = data.getIntExtra(CameraActivity.HEIGHT, -1);
            if (TextUtils.isEmpty(path)) {
                // ToastUtil.showToast("出现未知错误");
                return;
            }
            //  LogUtil.e("photo", "path = " + path + ", size = " + size + ", width = " + width + ", height = " + height);

            AlbumPhotoBean model = new AlbumPhotoBean();
            model.setPicture(path);
            model.setWidth(width);
            model.setHeight(height);
            model.setSize(size);

            final ArrayList<AlbumPhotoBean> list = new ArrayList();
            list.add(model);

            final RecyclerView recycler = findViewById(R.id.mine_case_add_picture);
            getPresenter().uploadPhoto(this, recycler, list, -1);
        }
    }
}